package com.admin.scroller.photoorvideo.holders;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.admin.scroller.photoorvideo.PerivewActivity;
import com.admin.scroller.photoorvideo.PhotoChoseActivity;
import com.admin.scroller.photoorvideo.R;
import com.admin.scroller.photoorvideo.beans.LocalPhoto;
import com.admin.scroller.photoorvideo.view.SquaredImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;

import java.io.File;

/**
 * 已grid界面形式展示照片数据的页面
 * 的 适配器 的 holder
 */
public class GridBodyHolder extends RecyclerView.ViewHolder {

    private View conteantView;
    SquaredImageView imageView;
    ImageView selected;
    private LinearLayout linearDuraction;
    private TextView textDuration;
    private static RequestManager manager;
    private static Context mcontext;

    private ChildGridHolder.OnImageItemClick imageItemClick;

    public static GridBodyHolder getHolder(Context context, ViewGroup parent,int type){
        View view=LayoutInflater.from(context).inflate(R.layout.layout_grild_item,parent,false);
        manager=Glide.with(context);
        mcontext=context;
        return new GridBodyHolder(view);
    }

    private GridBodyHolder(View itemView) {
        super(itemView);
        this.conteantView=itemView;
        imageView=conteantView.findViewById(R.id.iv_grild_item);
        selected=conteantView.findViewById(R.id.iv_selected);
        linearDuraction=conteantView.findViewById(R.id.linear_duration);
        textDuration=conteantView.findViewById(R.id.tv_duration);
    }

    public void setImageItemClick(ChildGridHolder.OnImageItemClick imageItemClick) {
        this.imageItemClick = imageItemClick;
    }

    public void initData(final LocalPhoto photo, final int position){
        String path;
        if(photo.isVideo()){
            path=photo.getVideoUrl();
            linearDuraction.setVisibility(View.VISIBLE);
            textDuration.setText(photo.getVideoDecation());
        }else {
            path=photo.getPath();
            linearDuraction.setVisibility(View.GONE);
        }
        if(!TextUtils.isEmpty(path)){
            File file=new File(path);
            if(file.exists()){
                manager.load(file).into(imageView);
            }
        }
        if(PhotoChoseActivity.viewModel.checkHasChosed(photo.getPath(),photo.getVideoUrl())){
            setSelectedImage(true);
        }else {
            setSelectedImage(photo.isSelected());
        }

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url;
                if(photo.isVideo()) {
                    url=photo.getVideoUrl();
                }else {
                    url=photo.getPath();
                }
                Intent intent = new Intent(mcontext, PerivewActivity.class);
                intent.putExtra(PerivewActivity.FILE_PATH, url);
                intent.putExtra(PerivewActivity.TYPE,1);
                intent.putExtra(PerivewActivity.INDEX,position);
                intent.putExtra(PerivewActivity.INDEX_PARENT,position);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    mcontext.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation((PhotoChoseActivity)mcontext, imageView, mcontext.getString(R.string.activity_show)).toBundle());
                }else {
                    mcontext.startActivity(intent);
                }
            }
        });

        selected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(imageItemClick!=null){
                    imageItemClick.onItemSelected(position,position);
                }
            }
        });
    }

    private void setSelectedImage(boolean s){
        if(s){
            selected.setImageResource(R.drawable.mis_btn_selected);
        }else {
            selected.setImageResource(R.drawable.mis_btn_unselected);
        }
    }
}
